package com.ask.controller.user;

import com.ask.dto.ShoppingCartDTO;
import com.ask.entity.ShoppingCart;
import com.ask.mapper.ShoppingCartMapper;
import com.ask.result.Result;
import com.ask.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 新增购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 获取用户的购物车数据
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> shoppingCartList =  shoppingCartService.showShoppingCart();
        return Result.success(shoppingCartList);
    }

    /**
     * 删除购物车中的一个商品
     * @param cartDTO
     */
    @PostMapping("/sub")
    public Result deleteItem(@RequestBody ShoppingCartDTO cartDTO) {
        shoppingCartService.deleteItem(cartDTO);
        return Result.success();
    }

    @DeleteMapping("/clean")
    public Result cleanShoppingCart() {
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

}
